﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace WebRaoVat.Admin
{
    public partial class QuanLyTinRaoVat : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LaAdmin"] == null || Session["LaAdmin"].ToString().Length <= 0)
            {                
                //Response.Redirect("../Default.aspx");
            }
            
        }

        /*protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            int maNgoNguNew = NgonNguBUS.getMa((string)e.NewValues[9]);
            int maDanhMucConNew = DanhMucConBUS.getMa((string) e.NewValues[8]);
            int maDiaDiemNew = DiaDiemBUS.getMa((string)e.NewValues[7]);

            TinRaoVatDTO tinraovat = new TinRaoVatDTO((int)e.NewValues[0], (string)e.NewValues[2],null,
                (int)e.NewValues[3],null, (DateTime)e.NewValues[4], (int)e.NewValues[5], maDiaDiemNew,
                (int)e.NewValues[6], maDanhMucConNew, maNgoNguNew, (int)e.NewValues[1] );

            if(TinRaoVatBUS.Update(tinraovat))
                throw new InvalidOperationException("Cập nhật tin rao thành công!");
            else
                throw new InvalidOperationException("Cập nhật tin rao không thành công!");

        } */

        /*protected void ASPxGridView2_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            throw new InvalidOperationException("No insert");
        }

        protected void ASPxGridView2_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            int ma = (int)e.Values[0];
            if (TinRaoVatBUS.Delete(ma))
            {
                throw new InvalidOperationException("Xóa thành công!");
                //ASPxGridView2.EndUpdate();
            }
            else
                throw new InvalidOperationException("Xóa không thành công, có thể tin này đã được xóa rồi!");
        }*/

        protected void ASPxGridView2_BeforePerformDataSelect(object sender, EventArgs e)
        {

        }

        protected void SqlDataSource2_Updating(object sender, SqlDataSourceCommandEventArgs e)
        {
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int maNgoNguNew = NgonNguBUS.getMa((string)e.NewValues[6]);
            int maDanhMucConNew = DanhMucConBUS.getMa((string)e.NewValues[7]);
            int maDiaDiemNew = DiaDiemBUS.getMa((string)e.NewValues[8]);

            string tentinrao = (string)e.NewValues[1];
            string thumbnail = null;
            int giatien = int.Parse(e.OldValues[2].ToString());
            try
            {
                giatien = int.Parse(e.NewValues[2].ToString());
            }
            catch (Exception)
            {
            }
            DateTime tgdang = DateTime.Parse(e.OldValues[3].ToString());
            try
            {
                tgdang = DateTime.Parse(e.NewValues[3].ToString());
            }
            catch (Exception)
            {
            }

            int solanxem = int.Parse(e.OldValues[4].ToString());
            try
            {
                solanxem = int.Parse(e.NewValues[4].ToString());
            }
            catch (Exception)
            {
            }
            /*int thoihanluutin = int.Parse(e.OldValues[9].ToString());
            try
            {
                thoihanluutin = int.Parse(e.NewValues[9].ToString());
            }
            catch (Exception)
            {
            }*/

            int manguodung = int.Parse(e.NewValues[5].ToString());
            int del = int.Parse(e.NewValues[0].ToString());
            int matinrao  = (int)e.Keys[0];

            TinRaoVatDTO tinraovat = new TinRaoVatDTO(matinrao, tentinrao,
                thumbnail, giatien, null, tgdang,
                solanxem, maDiaDiemNew, manguodung,
                maDanhMucConNew, maNgoNguNew, DateTime.Now, del);

            TinRaoVatBUS.Update(tinraovat);
            Server.Transfer("quanlytinraovat.aspx");
        }      
    }
}
